<?php
/**
 * 用户类
 */
namespace app\admin\controller;

use app\admin\model\Rolemodel;
use think\App;
use think\facade\View;
use app\admin\model\Usermodel;
use app\admin\validate\User as validate;

class Manageuser extends Common
{
    protected $model = 'User';
    private $userModel = '';
    private $roleModel = '';

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Usermodel();
        parent::__construct($app);
        $this->roleModel = new Rolemodel();
    }

    //管理端用户
    public function index(){
        $where = [];
        $username = $this->request->get('username', '');
        $mobile = $this->request->get('mobile', '');
        $rid = $this->request->get('rid', 0);
        $status = $this->request->get('status', 0);

        if($username)  $where[] = ['username', 'like', "%$username%"];
        if($mobile)  $where[] = ['mobile', 'like', "%$mobile%"];
        if($rid)  $where[] = ['rid', '=', $rid];
        if($status)  $where[] = ['u.status', '=', $status];

        $userRes = $this->localModel->findAllUserInfo($where);
        $roleRes = $this->roleModel->findAllInfo('id, name', [['id', 'not in', '1, 4, 8'], ['status', '=', 1], ['is_delete', '=', 1]]);

        View::assign(['userData'=>$userRes, 'roleRes'=>$roleRes, 'type'=>1, 'username'=>$username, 'mobile'=>$mobile, 'rid'=>$rid, 'status'=>$status]);
        return view('index');
    }

    //新增页面
    public function add(){
        $type = $this->request->get('type', 1);
        $id = $this->request->get('id', 0);
        $index = $this->request->get('index', 0);

        $roleRes = $this->roleModel->findAllInfo('id, name', [['id', 'NOT IN', '1, 4, 8'], ['status', '=', 1], ['is_delete', '=', 1]], ['id'=>'asc']);

        if($id){
            $userInfo = $this->localModel->findDetaiInfoById('*', $id);
            View::assign(['userInfo'=>$userInfo, 'id'=>$id, 'roleRes'=>$roleRes, 'type'=>$type, 'index'=>$index]);
            return view('edit');
        }else{
            View::assign(['roleRes'=>$roleRes, 'type'=>$type, 'index'=>$index]);
            return view('add');
        }
    }

    //数据入库
    public function save(){
        $dataArr = $this->request->request('user/a', []);
        $index = $this->request->post('index', 0);
        $id = $this->request->post('id', 0);

        if($dataArr['rid'])  $dataArr['admintype'] = 2;

        if($msg = $this->checkData($dataArr, $id ? ($id == 1 ? 'admin' : ($index ? 'uedit' : 'edit')) : 'add'))  return json(['result'=>'fail', 'title'=>$msg]);

        if($this->localModel->getUserInfoByName($dataArr['username'], $id))  return json(['result'=>'fail', 'title'=>'该用户名已存在！']);

        $res = $this->localModel->saveInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        if(!$index){
            $url = (string)url('Manageuser/index');
        }else{
            $url = (string)url('Index/index');
        }

        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['username']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>$url]);
    }
}
